package com.truth.lock.configure;

import com.truth.lock.template.LockTemplate;
import com.truth.lock.interceptor.TruthLockInterceptor;
import com.truth.lock.template.TruthLockTemplate;
import com.truth.lock.key.DefaultKeyGenerator;
import com.truth.lock.key.KeyGenerator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author truth
 */
@Configuration
@EnableConfigurationProperties(value = TruthLockProperties.class)
public class TruthLockAutoConfiguration {

    @Bean
    public LockTemplate lockTemplate(TruthLockProperties truthLockProperties) {
        return new TruthLockTemplate(truthLockProperties);
    }

    @Bean
    public TruthLockInterceptor truthLockInterceptor(LockTemplate lockTemplate, TruthLockProperties truthLockProperties) {
        return new TruthLockInterceptor(truthLockProperties, lockTemplate);
    }

    @Bean
    @ConditionalOnMissingBean(KeyGenerator.class)
    public KeyGenerator defaultKeyGenerator() {
        return new DefaultKeyGenerator();
    }

}
